﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IMS_Data;

namespace IMS_WEB
{
    public partial class UpdateCustomer : System.Web.UI.Page
    {

        string productName = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                productName = Request.QueryString["Name"];

                if (!string.IsNullOrEmpty(productName))
                {
                    Submit1.Value = "更新";

                    InitUI();
                }
            }

        }

        private void InitUI()
        {
           
            CompanyReposity cr = new CompanyReposity();
            company cp = cr.GetCompany(productName);

            ViewState.Add("ID", cp.index_id);
            tbCAddress.Value = cp.company_address;
            tbAccount.Value = cp.company_bankAccount;
            tbBankName.Value = cp.company_bankName;
            tbContact.Value = cp.company_contactName;
            tbContactPhone.Value = cp.company_contactPhone;
            tbFax.Value = cp.company_fax;
            tbMailCode.Value = cp.company_mailCode ;
            tbCName.Value = cp.company_name;
            tbCProxy.Value = cp.company_person;
            tbPhone.Value = cp.company_phone;
            tbCProxyName.Value = cp.company_proxy;
            tbRemark.Value = cp.remarks;
            tbTaxNumber.Value = cp.company_tax;
        }


        

        private void UpdateCompany()
        {
            company cp = GetCompany();

            CompanyReposity cr = new CompanyReposity();
            cr.UpdateCompany(cp);
            ClientScript.RegisterStartupScript(Submit1.GetType(), "", "<script>alert('更新成功!')</script>");

            Response.Redirect(string.Format("~/ViewCustomers.aspx"));
        }

        private company GetCompany()
        {
            company cp = new company();
            cp.company_address = tbCAddress.Value;
            cp.company_bankAccount = tbAccount.Value;
            cp.company_bankName = tbBankName.Value;
            cp.company_contactName = tbContact.Value;
            cp.company_contactPhone = tbContactPhone.Value;
            cp.company_fax = tbFax.Value;
            cp.company_mailCode = tbMailCode.Value;
            cp.company_name = tbCName.Value;
            cp.company_person = tbCProxy.Value;
            cp.company_phone = tbPhone.Value;
            cp.company_proxy = tbCProxyName.Value;
            cp.remarks = tbRemark.Value;
            cp.company_tax = tbTaxNumber.Value;
            cp.index_id = Convert.ToInt32(ViewState["ID"]);
            return cp;
        }

        protected void Submit1_Serverclick(object sender, EventArgs e)
        {
            
                UpdateCompany();
            
        }
    }
}